//
//  HomeViewController.swift
//  WB_Swift3.0
//
//  Created by wangrui on 2017/3/8.
//  Copyright © 2017年 wangrui. All rights reserved.
//

import UIKit


// 全局变量，前面加上 private 表示只能在该文件中使用
private let cellId = "cellId"

class HomeViewController: BaseViewController {
    
    fileprivate lazy var statusList = [String]()
    
    @objc fileprivate func clickFriends()
    {
        let vc = DemoViewController()
        navigationController?.pushViewController(vc, animated: true)
    }
    
    override func loadData()
    {
        print("正在刷新数据")
        DispatchQueue.main.asyncAfter(deadline: .now() + 1, execute:
        {
            if self.mIsPull == true
            {
                for i in 0..<10 {
                    self.statusList.append("上拉加载更多：\(i)")
                }
            }
            else
            {
                for i in 0..<15 {
                    self.statusList.insert("下：\(i)", at: 0)
                }
            }
            
            self.mIsPull = false
            // 下拉刷新控件停止刷新
            self.refreshControl?.endRefreshing()
            self.tableView?.reloadData()
        })
    }
    
}


// MARK: - tableViewDataSource , tableViewDelegate
extension HomeViewController
{
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return statusList.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {
        let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath)
        cell.textLabel?.text = statusList[indexPath.row]
        return cell
    }
}

extension HomeViewController
{
    override func setupTableView()
    {
        super.setupTableView()
        navItem.leftBarButtonItem = UIBarButtonItem(title: "好友", target: self, action: #selector(clickFriends))
        tableView?.register(NSClassFromString("UITableViewCell"), forCellReuseIdentifier: cellId)
    }
}
